<?php 
    session_start();
    include_once 'inc/db.class.php';
    include_once 'inc/function.php';
    include_once 'sesslon.php';
    
    //读取该用户购物车的产品
        $sql="select basket.*,business.name from basket,business where basket.bid=business.id and mid=".getmid($_SESSION["jjusername"])." and flag=0";
    $rs=$db->getData($sql);
    //print_r($rs);
    //格式化数字
    $rsbusiness=array();
    foreach ($rs as $v){
        $rsbusiness[$v["bid"]]=$v["name"];
    }
    //print_r($rsbusiness);
    
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<?php include_once 'meta.php'; ?>

		<link href="assets/css/amazeui.css" rel="stylesheet" type="text/css" />
		<link href="basic/css/demo.css" rel="stylesheet" type="text/css" />
		<link href="css/cartstyle.css" rel="stylesheet" type="text/css" />
		<link href="css/optstyle.css" rel="stylesheet" type="text/css" />

		<script src="http://www.5imoban.net/download/jquery/jquery.1.11.3.js"></script>
		<script type="text/javascript" src="js/jquery-3.2.1.js"></script>
		<script type="text/javascript" src="layer/layer.js"></script>
		<script>
			function setdefault(id){
					alert(id);
				}

		</script>
		
        <script>
			function addfav(id,name,pic){
				$.ajax({
					url:"ajax/fav.php",
					type:"post",
					data:{"id":id,"name":name,"pic":pic},
					success:function(data){
						if(data==0){
							layer.msg('请登录后操作',{icon:5});
						}else if(data==1){
							layer.msg('收藏成功',{icon:6});
						}else if(data==2){
							layer.msg('收藏失败3',{icon:5});
						}else if(data==3){
							layer.msg('该产品您已经收藏过了，亲，不要重复收藏',{icon:6});
						}else{
							layer.msg('收藏失败2',{icon:5});
						}
					},
					error:function(){
						layer.msg('收藏失败',{icon:5});
					}
				});
			}
			function del(id,bid){
				layer.confirm('是否删除数据？', {
					  btn: ['是','否'] //按钮
					}, function(){
						$.ajax({
							url:"ajax/del.php",
							type:"post",
							data:{"id":id},
							success:function(data){
								if(data==0){
									layer.msg('请登录后操作',{icon:5});
								}else if(data==1){
									layer.msg('参数错误，删除失败',{icon:5});
								}else if(data==2){
									
									
									layer.msg('删除成功',{icon:6});
									/*setTimeout(function(){
										window.location.reload();
									},1000);*/
									$("#sc"+id).remove();
									//判断该商家下订单是否全部都删除了
									
									if($("#sctr"+bid).children(".bundle-last").children(".bundle-main").children("ul").length<=0){
										$("#sctr"+bid).remove();
									}
									
								}else if(data==3){
									layer.msg('删除失败1',{icon:5});
								}else{
									layer.msg('删除失败2',{icon:5});
								}
							},
							error:function(){
								layer.msg('删除失败3',{icon:5});
							}
						});
					}, function(){
					  
					});
				
			}

			$(document).ready(function(){
				$(".ordercheck").click(function(){
					getsum();
				});
				$("#J_SelectAllCbx2").click(function(){
					$(".ordercheck").prop("checked",$(this).prop("checked"));
					getsum();
				});
				$("#delAll").click(function(){
					var str="";
					for(var i=0;i<$(".ordercheck").length;i++){
						if($(".ordercheck").eq(i).prop("checked")){
							str+=$(".ordercheck").eq(i).val()+",";
						}
					}
					if(str==""){
						layer.msg("请勾选需要删除的订单",{icon:5});
						return;
					}
					$.ajax({
						url:"ajax/del.php",
						type:"post",
						data:{"id":str,"flag":"all"},
						success:function(data){
							if(data==0){
								layer.msg('请登录后操作',{icon:5});
							}else if(data==1){ 
								layer.msg('参数错误，删除失败',{icon:5});
							}else if(data==2){
								
								
								layer.msg('删除成功',{icon:6});
								str=str.substring(0,str.length-1);
								var arr=str.split(",");
								for(var m=0;m<arr.length;m++){
									$("#sc"+arr[m]).remove();
								}
								
								//判断全部商家下订单是否全部都删除了
								var nn=$(".item-list").length;
								for(var n=nn-1;n>=0;n--){
									
									if($(".item-list").eq(n).children(".bundle-last").children(".bundle-main").children("ul").length<=0){
										$(".item-list").eq(n).remove();
									}
								}
								
								
							}else if(data==3){
								layer.msg('删除失败1',{icon:5});
							}else{
								layer.msg('删除失败2',{icon:5});
							}
						},
						error:function(){
							layer.msg('删除失败3',{icon:5});
						}
					});
				});
				//加 减方法
				$(".add,.min").click(function(){
					var num;
					if($(this).val()=="+"){
						var a=parseInt($(this).prev().val());
						$(this).prev().val(a+1);
						num=$(this).prev();
					}
					if($(this).val()=="-"){
						var a=parseInt($(this).next().val());
						if(a>1){
							$(this).next().val(a-1);
						}
						num=$(this).next();
					}
					var t=num.val()*$("#j_price"+num.attr("orderid")).text();
					t=t.toFixed(2);
					$("#t_price"+num.attr("orderid")).text(t);
					getsum();
					
				});
				$(".text_box").keyup(function(){
					if(isNaN($(this).val()) || $(this).val()<=0){
						$(this).val(1);
					}
					var t=$(this).val()*$("#j_price"+$(this).attr("orderid")).text()
					$("#t_price"+$(this).attr("orderid")).text(t);
					getsum();
				});

				//***全选   单个复选框    购买数量文本框键盘弹起    加减**
				/******
						（1）检测 复选框被选中几个  等到选中几个产品
						（2）在选中状态获取每个选中订单的 单价和购买数量 相乘  汇总 得到总金额
				******/
				
			});
			function getsum(){
				var c=0;
				var sum=0;
				for(var i=0;i<$(".ordercheck").length;i++){
					if($(".ordercheck").eq(i).prop("checked")){
						++c;
						var id=$(".ordercheck").eq(i).val();
						var p=$("#t_price"+id).text();
						sum+=parseFloat(p);
					}
					
				}
				$("#J_SelectedItemsCount").text(c);
				$("#J_Total").text(sum);
			}

			//结算单击事
			$(document).ready(function(){
				$("#J_Go").click(function(){
					var n=0;
					var strid="";
					var strnum="";
					for(var i=0;i<$(".ordercheck").length;i++){
						if($(".ordercheck").eq(i).prop("checked")){
							++n;
							for(var m=0;m<$(".text_box").length;m++){
								if($(".text_box").eq(m).attr("orderid")==$(".ordercheck").eq(i).val()){
									strnum+=$(".text_box").eq(m).val()+",";
									break;
								}
							}
							strid+=$(".ordercheck").eq(i).val()+",";
						}
					}
					
					if(n<=0){
						layer.msg("请勾选需要结算的产品",{icon:5});
						return;
					}
					//执行ajax 产品id 购买数量
					$.ajax({
						url:"ajax/pay.php",
						type:"post",
						data:{"strid":strid,"strnum":strnum},
						success:function(data){
							if(data==0){
								layer.msg("请登录后操作",{icon:5});
							}else if(data==1){
								layer.msg("结算失败,数据错误",{icon:5});
							}else if(data==2){
								$("#oid").val(strid);
								//提交表单
								$("#f1").submit();
							}else if(data==3){
								layer.msg("结算失败2",{icon:5});
							}else{
								layer.msg("结算失败1",{icon:5});
							}
						},
						error:function(){
							layer.msg("error 稍后结算",{icon:5});
						}
					});
				});
			});
        </script>
	</head>

	<body>

		  

			<?php include_once 'header1.php'; ?>

			<div class="clear"></div>

			<!--购物车 -->
			<div class="concent">
				<div id="cartTable">
					<div class="cart-table-th">
						<div class="wp">
							<div class="th th-chk">
								<div id="J_SelectAll1" class="select-all J_SelectAll">

								</div>
							</div>
							<div class="th th-item">
								<div class="td-inner">商品信息</div>
							</div>
							<div class="th th-price">
								<div class="td-inner">单价</div>
							</div>
							<div class="th th-amount">
								<div class="td-inner">数量</div>
							</div>
							<div class="th th-sum">
								<div class="td-inner">金额</div>
							</div>
							<div class="th th-op">
								<div class="td-inner">操作</div>
							</div>
						</div>
					</div>
					<div class="clear"></div>
                    <?php foreach ($rsbusiness as $k=>$v){ ?>
					<div id="sctr<?php echo $k; ?>" class="item-list">
						<div class="bundle  bundle-last ">
							<div class="bundle-hd">
								<div class="bd-promos">
									<div class="bd-has-promo"><?php echo $v; ?></div>
								</div>
							</div>
							<div class="clear"></div>
							<div class="bundle-main">
								<?php foreach ($rs as $v2){ 
								      if($k!=$v2["bid"]){
								            continue;
								      }
								?>
								<ul class="item-content clearfix" id="sc<?php echo $v2["id"]; ?>">
									<li class="td td-chk">
										<div class="cart-checkbox ">
											<input class="check ordercheck"  value="<?php echo $v2["id"]; ?>" type="checkbox">
											<label for="J_CheckBox_170037950254"></label>
										</div>
									</li>
									<li class="td td-item">
										<div class="item-pic">
											<a href="introduction.php?id=<?php echo $v2["gid"]; ?>" target="_blank" data-title="<?php echo $v2["gname"]; ?>" class="J_MakePoint" data-point="tbcart.8.12">
												<img height="80" width="80" src="goodsimg/<?php echo $v2["gpic"]; ?>" class="itempic J_ItemImg"></a>
										</div>
										<div class="item-info">
											<div class="item-basic-info">
												<a href="introduction.php?id=<?php echo $v2["gid"]; ?>" target="_blank" title="<?php echo $v2["gname"]; ?>" class="item-title J_MakePoint" data-point="tbcart.8.11"><?php echo $v2["gname"]; ?></a>
											</div>
										</div>
									</li>
									<li class="td td-info">
										<div class="item-props item-props-can">
											<span class="sku-line"><?php echo $v2["gspec"]; ?></span>
											<i class="theme-login am-icon-sort-desc"></i>
										</div>
									</li>
									<li class="td td-price">
										<div class="item-price price-promo-promo">
											<div class="price-content">
												
												<div class="price-line">
													<em id="j_price<?php echo $v2["id"]; ?>" class="J_Price price-now" tabindex="0"><?php echo $v2["gpirce"]; ?></em>
												</div>
											</div>
										</div>
									</li>
									<li class="td td-amount">
										<div class="amount-wrapper ">
											<div class="item-amount ">
												<div class="sl">
													<input class="min am-btn" name="" type="button" value="-" />
													<input class="text_box" name="" orderid="<?php echo $v2["id"]; ?>" type="text" value="<?php echo $v2["gnum"]; ?>" style="width:30px;" />
													<input class="add am-btn" name="" type="button" value="+" />
												</div>
											</div>
										</div>
									</li>
									<li class="td td-sum">
										<div class="td-inner">
											<em tabindex="0" id="t_price<?php echo $v2["id"]; ?>" class="J_ItemSum number"><?php echo number_format($v2["gnum"]*$v2["gpirce"],2,".",""); ?></em>
										</div>
									</li>
									<li class="td td-op">
										<div class="td-inner">
											<a onclick="addfav(<?php echo $v2["gid"]; ?>,'<?php echo $v2["name"]; ?>','<?php echo $v2["gpic"]; ?>')" title="移入收藏夹" class="btn-fav" href="javascript:;"> 移入收藏夹</a>
											<a onclick="del(<?php echo $v2["id"];?>,<?php echo $k; ?>)" href="javascript:;" >删除</a>
										</div>
									</li>
								</ul>
								<?php } ?>
								
							</div>
						</div>
					</div>
					<?php } ?>
					<div class="clear"></div>

					
				</div>
				<div class="clear"></div>

				<div class="float-bar-wrapper">
					<div id="J_SelectAll2" class="select-all J_SelectAll">
						<div class="cart-checkbox">
							<input  class="check-all check" id="J_SelectAllCbx2" name="select-all" value="true" type="checkbox">
							<label for="J_SelectAllCbx2"></label>
						</div>
						<span>全选</span>
					</div>
					<div class="operations">
						<a href="javascript:;" id="delAll" hidefocus="true" class="deleteAll">删除</a>
						
					</div>
					<div class="float-bar-right">
						<div class="amount-sum">
							<span class="txt">已选订单</span>
							<em id="J_SelectedItemsCount">0</em><span class="txt">个</span>
							<div class="arrow-box">
								<span class="selected-items-arrow"></span>
								<span class="arrow"></span>
							</div>
						</div>
						<div class="price-sum">
							<span class="txt">合计:</span>
							<strong class="price">¥<em id="J_Total">0.00</em></strong>
						</div>
						<div class="btn-area">
							<a href="javascript:;" id="J_Go" class="submit-btn submit-btn-disabled" aria-label="请注意如果没有选择宝贝，将无法结算">
								<span>结&nbsp;算</span></a>
						</div>
					</div>

				</div>

				<?php include_once 'footer.php';?>

			</div>

			<!--操作页面-->

			<div class="theme-popover-mask"></div>

			<div class="theme-popover">
				<div class="theme-span"></div>
				<div class="theme-poptit h-title">
					<a href="javascript:;" title="关闭" class="close">×</a>
				</div>
				<div class="theme-popbod dform">
					<form class="theme-signin" name="loginform" action="" method="post">

						<div class="theme-signin-left">

							<li class="theme-options">
								<div class="cart-title">颜色：</div>
								<ul>
									<li class="sku-line selected">12#川南玛瑙<i></i></li>
									<li class="sku-line">10#蜜橘色+17#樱花粉<i></i></li>
								</ul>
							</li>
							<li class="theme-options">
								<div class="cart-title">包装：</div>
								<ul>
									<li class="sku-line selected">包装：裸装<i></i></li>
									<li class="sku-line">两支手袋装（送彩带）<i></i></li>
								</ul>
							</li>
							<div class="theme-options">
								<div class="cart-title number">数量</div>
								<dd>
									<input class="min am-btn am-btn-default" name="" type="button" value="-" />
									<input class="text_box" name="" type="text" value="1" style="width:30px;" />
									<input class="add am-btn am-btn-default" name="" type="button" value="+" />
									<span  class="tb-hidden">库存<span class="stock">1000</span>件</span>
								</dd>

							</div>
							<div class="clear"></div>
							<div class="btn-op">
								<div class="btn am-btn am-btn-warning">确认</div>
								<div class="btn close am-btn am-btn-warning">取消</div>
							</div>

						</div>
						<div class="theme-signin-right">
							<div class="img-info">
								<img src="images/kouhong.jpg_80x80.jpg" />
							</div>
							<div class="text-info">
								<span class="J_Price price-now">¥39.00</span>
								<span id="Stock" class="tb-hidden">库存<span class="stock">1000</span>件</span>
							</div>
						</div>

					</form>
				</div>
			</div>
		<!--引导 -->
		<div class="navCir">
			<li><a href="home2.php"><i class="am-icon-home "></i>首页</a></li>
			<li><a href="sort.php"><i class="am-icon-list"></i>分类</a></li>
			<li class="active"><a href="shopcart.php"><i class="am-icon-shopping-basket"></i>购物车</a></li>	
			<li><a href="person/index.php"><i class="am-icon-user"></i>我的</a></li>					
		</div>
		<form id="f1" method="post" action="pay.php">
		      <input type="hidden" id="oid" name="oid" value="" />
		</form>
	</body>

</html>










